﻿using Generic.App.Domain.Entity;
using Generic.Domain.Shared.Exception;
using Generic.Domain.Shared.Const;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Domain.Services;

namespace Generic.App.Domain.Manager
{
    public class App_TokenGreyManager : DomainService
    {
        private readonly IRepository<App_TokenGrey, string> _tokenGreyRepository;
        public App_TokenGreyManager(IRepository<App_TokenGrey, string> tokenGreyRepository)
        {
            _tokenGreyRepository = tokenGreyRepository;
        }

        public async Task Check_UniqueAsync(App_TokenGrey entity)
        {
            await Check_UniqueTokenAsync(entity.Id, entity.Token);
        }

        private async Task Check_UniqueTokenAsync(string id, string token)
        {
            if ((await _tokenGreyRepository.GetQueryableAsync()).Where(p => p.Token == token && p.Id != id).Any())
                throw new CustomFriendlyException(nameof(BackErrorConst.TokenAlreadyExists), BackErrorConst.TokenAlreadyExists);
        }
    }
}
